<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * Main class for working with site pages
     */
    class posts extends CI_Controller {

        private $data;

        function __construct() {

            parent::__construct();

            if (!$this->session->userdata('logged_in'))
                redirect(base_url('login'));

            $this->load->config('languages');
            $this->load->model('posts_model');

            $this->translate = & $this->factory->create('translate', 'foo', 'bar');
            $this->activeLang = $this->session->userdata('my_language');
        }

        public function index() {
            if (!isset($_GET['post_type']))
                $_GET['post_type'] = "page";

            $this->view_all();
        }

        /**
         * @param $_GET['post_type']
         */
        function view_all() {
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'View all ' . $_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type'];

            $this->template->load('posts/view_all', $this->data);
        }

        /**
         * 
         * @param $_GET['post_ID']
         * @param $_GET['post_type']
         */
        function view_edit() {
            $this->data['post_action'] = $_GET['post_action'];
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'View ' . $_GET['post_type'];

            $posts = $this->posts_model->view_all($_GET['post_type']);
            $this->data['all_posts'] = $this->translate->res_array($posts, $this->activeLang);

            if ($_GET['post_action'] == 'EDIT')
                $this->data['post'] = $this->posts_model->view_edit($_GET['post_ID']);


            $this->template->load('posts/edit_post', $this->data);
        }

        /**
         * 
         * @param $_POST['parent_id']
         * @param $_POST['post_type']
         * @param $_POST['post_title']
         * @param $_POST['post_action']
         * @param $_POST['content']
         * @param $_POST['qtrans_textarea_content']
         */
        function submit_post() {

            $_POST['post_title'] = $this->colparse->regular_array_to_col($_POST['title']);
            $_POST['content'] = $this->colparse->regular_array_to_col($_POST['content']);

            // Clear html special character from title if exist
            $_POST['post_title'] = str_replace('"', '', $_POST['post_title']);

            $some_title = $this->strparse->find_lang_in_string($_POST['post_title'], 'en');

            switch ($_POST['post_action']) {

                case 'NEW':

                    $result = $this->posts_model->create_page(
                            $_POST['post_title'], $some_title, $_POST['content'], $_POST['parent_id'], $_POST['post_type']
                    );

                    $message = 'Successful insert operation';

                    break;

                case 'EDIT':

                    $result = $this->posts_model->update_page(
                            $_POST['post_ID'], $_POST['post_title'], $some_title, $_POST['content'], $_POST['parent_id'], $_POST['post_type']
                    );

                    $message = 'Successful edit operation';

                    break;
            }

            // Return
            if ($result) {
                echo json_encode(array('action' => true, 'msg' => $message));
            } else {
                echo json_encode(array('action' => true, 'msg' => 'Something went wrong. Please try again.'));
            }
        }

        function delete($ID) {

            if ($this->posts_model->delete_page($ID)) {
                $this->view_all();
            } else {
                show_error('Error ocured while deleting page!');
            }
        }

    }

?>
